// pages/attractionsComment/attractionsComment.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reviews: [],
    spotId: ""
  },
  onClickLeft(){
    wx.navigateBack();
  },
  onLoad(options) {
    if (options && options.id) {
      const Id = options.id;
      this.setData({ spotId: Id });
      this.getComment(Id);
    }
  },

  getComment(spotId) {
    const app = getApp();
    const baseUrl = 'http://localhost:8080';
    wx.request({
      url: 'http://localhost:8080/system/comment/list',
      header: {
        Authorization: app.globalData.token
      },
      data: {
        aid: spotId
      },
      success: (res) => {
        if (res.data.code === 200 && Array.isArray(res.data.rows)) {
          const commentList = res.data.rows
            .filter(item => item.state === 1)
            .map(item => ({
              avatar: baseUrl + item.avatar,
              nickname: item.nickName,
              rating: item.star,
              content: item.content,
              date: item.time.split(' ')[0],
              imageList: item.imgUrl ? item.imgUrl.split(',').map(url => baseUrl + url) : []
            }));
  
          this.setData({
            reviews: commentList
          });
        } else {
          wx.showToast({ title: '暂无评论信息', icon: 'none' });
        }
      },
      fail: () => {
        wx.showToast({ title: '网络请求失败', icon: 'none' });
      }
    });
  }
});
